﻿using UnityEngine;
using System.Collections;
//Enum for the GameState,if won, lose or playing
public enum GameState {playing, won, lost };

public class Manager : MonoBehaviour {

	public Manager SP;
    //check if won
	bool Won = false;
    //check if lost
	bool Lost = false;
    //to manager of states
	private GameState gameState;
    //Check if is paused
	public bool paused = false;

	public int cont =0;
    // Is called before Start(). Great for references.
    void awake()
    {
        //velocity of the game for standard is 1 
		Time.timeScale = 0.0f;
		SP = this;
    }
	// Use this for initialization
	void Start () {
	
	}
	// Update is called once per frame
	void Update () 
	{
		cont++;
		if (cont >= 200)
		{
			Time.timeScale = 1.0f;
			Debug.Log("Comecou");
		}
        //method to pause the game
		PauseGame();


	
	}
    //To text and buttons in screen
	void OnGUI ()
	{
        //Begin of gui group,where have size and position
		GUI.BeginGroup(new Rect(Screen.width/2 - 50, Screen.height/2 - 100, 300, 500));
        //Gui color white
		GUI.color = Color.white;
        //if the gameState equal lost
		if(gameState == GameState.lost)
		{
            //in the screen the message lost
			GUILayout.Label("You Lost!");
            //the var lost is true
			Lost = true;
            //if click in button 
			if(GUILayout.Button("Try Again",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //the scene will go reset
				Application.LoadLevel(Application.loadedLevel);
			}
            //if click in button 
			if(GUILayout.Button("Back to Menu",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //go to menu
				Application.LoadLevel(0);
			}
		}

         //if the gameState equal won
		else if(gameState == GameState.won)
		{
            //in the screen the message win
			GUILayout.Label("You Win!");
            //the var won is true
            Won = true;
            //if click in button 
			if(GUILayout.Button("Restart",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //the scene will go reset
				Application.LoadLevel(Application.loadedLevel);
			}
            //if click in button 
			if(GUILayout.Button("Back to Menu",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //go to menu
				Application.LoadLevel(0);
			}
			
		}
		//if is paused
		if(paused)
		{
            //if click in button 
			if(GUILayout.Button("Continue",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //the timeScale 1 to the game run
				Time.timeScale = 1.0f;
                // the var paused is false now
				paused = false;
			}
            //if click in button 
			else if(GUILayout.Button("Restart",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //the scene will go reset
				Application.LoadLevel(Application.loadedLevel);
                //the timeScale 1 to the game run
				Time.timeScale = 1.0f;
                // the var paused is false now
				paused = false;
			}
            //if click in button 
			else if(GUILayout.Button("Back to Menu",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //the scene will go reset
				Application.LoadLevel(0);
			}
            //if click in button 
			else if(GUILayout.Button("Exit",GUILayout.Width(150),GUILayout.Height(50)))
			{
                //Exit of game
				Application.Quit();
			}	
			
		}
        //End of Gui Group
		GUI.EndGroup();
	}
    //method to pause the game
	public void PauseGame()
	{ 
        //if pressed esc and no paused,no won and no lost
		if(Input.GetKeyDown("escape")&& !paused&&!Won&&!Lost)
		{
            //the timeScale 0 to pause the game 
			Time.timeScale = 0.0f;
            // the var paused is true now
			paused = true;
		}
        //if pressed esc and paused,no won and no lost
		else if(Input.GetKeyDown("escape") && paused&&!Won&&!Lost)
		{
            //the timeScale 1 to the game run
			Time.timeScale = 1.0f;
            // the var paused is false now
			paused = false;	
		} 
	}
}
